package com.lagou.edu.jdbc;

import com.lagou.edu.annotation.Autowired;
import com.lagou.edu.annotation.Component;

import java.sql.SQLException;

/**
 * @author fanbo@imoran.net
 * @date 2020/12/25 下午8:23
 */
@Component
public class TransactionManager {

    @Autowired
    private ConnectionUtils connectionUtil;

    /**
     * 开启手动事务控制
     * @throws SQLException
     */
    public void beginTransaction() throws SQLException {
        System.out.println("开启手动事务控制...");
        connectionUtil.getCurrentThreadConn().setAutoCommit(false);
    }


    /**
     * 提交事务
     * @throws SQLException
     */
    public void commit() throws SQLException {
        System.out.println("事务提交...");
        connectionUtil.getCurrentThreadConn().commit();
    }


    /**
     * 回滚事务
     * @throws SQLException
     */
    public void rollback() throws SQLException {
        System.out.println("事务回滚...");
        connectionUtil.getCurrentThreadConn().rollback();
    }
}
